//
//  CarouselView.m
//  TotalDemo
//
//  Created by gujian on 16/5/13.
//  Copyright © 2016年 gujian. All rights reserved.
//

#import "CarouselView.h"

static const CGFloat marginLeft = 15.0;
static const CGFloat kCarousHeight = 34.0;

@interface CarouselView()<UIScrollViewDelegate>

@property (nonatomic, weak) UIScrollView *contentScrollView;
@property (nonatomic, strong) NSArray<UIView *> *viewsArr;
@property (nonatomic, assign) NSInteger currentPageIndex;
@property (nonatomic, assign) CGFloat preDeltaX;
@property (nonatomic, assign) CGFloat preOffX;

@end

@implementation CarouselView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        _currentPageIndex = 0;
        [self setupSubView];
    }
    
    return self;
}

- (void)setupSubView
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    scrollView.backgroundColor = [UIColor whiteColor];
    scrollView.delegate = self;
    scrollView.pagingEnabled = YES;
    scrollView.showsVerticalScrollIndicator = NO;
//    scrollView.showsHorizontalScrollIndicator = NO;
    [scrollView setScrollsToTop:NO];
    [scrollView setAlwaysBounceHorizontal:NO];
    [scrollView setAlwaysBounceVertical:NO];
    [scrollView setBounces:NO];
    [self addSubview:scrollView];
    _contentScrollView = scrollView;
    
    NSArray *colorArr = @[[UIColor redColor],[UIColor yellowColor],[UIColor orangeColor]];
    
    CGFloat margin = 15.0;
    CGFloat viewWidth = self.width - 2 * margin;
    CGFloat offX = margin;
    NSMutableArray *tempArr = [NSMutableArray arrayWithCapacity:colorArr.count];
    for (int i = 0; i < 3; i++) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(offX, 0, viewWidth, self.height)];
        if (i > 0) {
            view.height -= kCarousHeight;
        }
        view.centerY = scrollView.height/2.0;
        view.backgroundColor = colorArr[i];
        
        [scrollView addSubview:view];
        offX += viewWidth;//scrollView.width;//
        [tempArr addObject:view];
    }
    
    _viewsArr = [tempArr copy];
    scrollView.contentSize = CGSizeMake(self.width * colorArr.count, 0);
    
}





#pragma mark - UIScrollView delegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{

    [self updateScrollView];
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    NSInteger pageIndex = scrollView.contentOffset.x / scrollView.frame.size.width;
    if (pageIndex != _currentPageIndex) {
//        _currentPageIndex = pageIndex;
//        [self updateScrollView:pageIndex];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger pageIndex = scrollView.contentOffset.x / scrollView.frame.size.width;
    if (pageIndex != _currentPageIndex) {
        _currentPageIndex = pageIndex;
        [self updateScrollView:pageIndex];
    }
}

//最终形态
- (void)updateScrollView:(NSInteger )pageIndex
{
    UIView *preView =  nil;
    UIView *nextView = nil;
    CGFloat viewWidth = self.width - 2 * marginLeft;
    CGFloat offX = marginLeft*(2 * (pageIndex+1)-1) + viewWidth * pageIndex;
    if (pageIndex > 0) {
        preView = _viewsArr[pageIndex-1];
    }
    
    CGFloat deltaHeight = kCarousHeight * (1);
    
    if (preView != nil){
        preView.left += marginLeft;
//        pageIndex > 0? marginLeft : marginLeft*(2 * (pageIndex)-1) + viewWidth * pageIndex;
        preView.width = _contentScrollView.width - marginLeft;
//        NSLog(@"preView.width:%@",@(preView.width));
        preView.height = (_contentScrollView.height - deltaHeight);
        preView.centerY = _contentScrollView.height/2.0;
    }
    
    UIView *currentView = _viewsArr[pageIndex];
    if (currentView != nil){
        currentView.left = offX;
        currentView.width = _contentScrollView.width - 2 * marginLeft;
        NSLog(@"currentView.width:%@",@(currentView.width));
        currentView.height = _contentScrollView.height;
        currentView.centerY = _contentScrollView.height/2.0;
    
    }
    
    if (preView != nil && preView.height <= currentView.height) {
    }
    
    if (pageIndex < (_viewsArr.count-1)) {
        nextView = _viewsArr[pageIndex + 1];
    }
    
    if (nextView) {
        nextView.left = marginLeft*(2 * (pageIndex+1)-1) + viewWidth * (pageIndex+1);
        nextView.width = _contentScrollView.width - marginLeft;
//        NSLog(@"nextView.width:%@",@(nextView.width));
        nextView.height =  (_contentScrollView.height - deltaHeight);
        nextView.centerY = _contentScrollView.height/2.0;
    }
    
    currentView.alpha = 0.8;
    [_contentScrollView bringSubviewToFront:currentView];

}

//滑动中的形态
- (void)updateScrollView
{
    UIView *preView =  nil;
    UIView *nextView = nil;
    NSInteger pageIndex = _contentScrollView.contentOffset.x / (_contentScrollView.frame.size.width);
    BOOL isJumped = NO;
    if (pageIndex == _currentPageIndex) {
        isJumped = NO;
    }else
        isJumped = YES;
    CGFloat viewWidth = self.width - 2 * marginLeft;
    if (pageIndex > 0) {
        preView = _viewsArr[pageIndex-1];
    }
    

    CGFloat deltaX = _contentScrollView.contentOffset.x - _contentScrollView.frame.size.width * pageIndex;
    CGFloat deltaHeight = kCarousHeight * (deltaX/(_contentScrollView.frame.size.width));
    
    NSLog(@"pageIndex:%@",@(pageIndex));
    
    UIView *currentView = _viewsArr[pageIndex];
    if (currentView != nil){
        
        CGFloat height = self.height - deltaHeight;
        if (height < (self.height - kCarousHeight)) {
            height = self.height - kCarousHeight;
        }
        currentView.height = height;
        currentView.centerY = _contentScrollView.height/2.0;
        
    }
    
    //需要动态改变preView的left
    if (preView != nil){

        preView.centerY = _contentScrollView.height/2.0;
        
//        if (preView.height <= currentView.height) {
//            CGFloat preViewLeft = preView.left - deltaX;
//            if (preViewLeft >= pageIndex > 0? marginLeft : marginLeft*(2 * (pageIndex)-1) + viewWidth * pageIndex) {
//                preViewLeft = pageIndex > 0? marginLeft : marginLeft*(2 * (pageIndex)-1) + viewWidth * pageIndex;
//            }
//            NSLog(@"preView.left:%@",@(preView.left));
//            preView.left = preViewLeft;
//
//        }
    }
    
    if (pageIndex < (_viewsArr.count-1)) {
        nextView = _viewsArr[pageIndex + 1];
    }
    
    if (nextView) {
        
        CGFloat height = (self.height-kCarousHeight) + deltaHeight;
        if (nextView.height > self.height) {
            height = self.height;
        }
        nextView.height = height;
        nextView.centerY = _contentScrollView.height/2.0;
        
        if (nextView.height >= currentView.height) {
            
            CGFloat nextViewLeft = nextView.left + deltaX;
            CGFloat targetOffX = marginLeft*(2 * (pageIndex+2)-1) + viewWidth * (pageIndex+1);
            if (nextViewLeft >= targetOffX) {
                nextViewLeft = targetOffX;
            }
            NSLog(@"1nextView.left:%@",@(nextView.left));
//            nextView.left = nextViewLeft;
             NSLog(@"2nextView.left:%@",@(nextView.left));
            
            
            //当前视图
            CGFloat currentViewLeft = currentView.left + deltaX;
//            NSLog(@"deltaX:%@",@(deltaX));
            targetOffX = marginLeft*(2 * (pageIndex+2)-1) + viewWidth * pageIndex;
            if (deltaX <= _preDeltaX)
            {

                //右拉  isJumped ||
        
                
            }else
            {
                //左拉
                targetOffX = marginLeft*(2 * (pageIndex+1)-1) + viewWidth * pageIndex;

                
            }
            if (currentViewLeft >= targetOffX) {
                currentViewLeft = targetOffX;
            }
//                currentView.left = currentViewLeft;
            

//            NSLog(@"currentView.left:%@",@(currentView.left));
            
            
            //前一个视图
            if (preView){
                CGFloat preViewLeft = preView.left + deltaX;
                CGFloat targetOffX = pageIndex > 0? marginLeft*(2 * (pageIndex+1)-1) + viewWidth * (pageIndex-1):marginLeft;
                if (deltaX < _preDeltaX)
                {
                    //右滑

                }else
                {
                    //左滑
                }
                if (preViewLeft >= targetOffX) {
                    preViewLeft = targetOffX;
                }
                
//                    preView.left = preViewLeft;
//                NSLog(@"preView.left:%@",@(preView.left));
            }
            
            
        }
    }

        
        

    
    
    currentView.alpha = 0.8;
    if ((preView && preView.height <= currentView.height) || (nextView && nextView.height <= currentView.height)) {
        [_contentScrollView bringSubviewToFront:currentView];
    }
    

    
    _currentPageIndex = pageIndex;
    _preDeltaX = deltaX;
    _preOffX = _contentScrollView.contentOffset.x;
    
}

- (void)setSelectedIndex:(NSInteger )pageIndex
{
    
}



@end
